

/**
 * @param {function} isBadVersion()
 * @return {function}
 */
 var solution = function(isBadVersion) {
  /**
   * @param {integer} n Total versions
   * @return {integer} The first bad version
   */
  return function(n) {
    let low = 0, mid = Math.floor(n / 2), high = n;
    while(low < high) {
      if(isBadVersion(mid)) {
        high = mid - 1;
        if(!isBadVersion(high)) return high + 1;
      } else {
        low = mid + 1;
        if(isBadVersion(low)) return low;
      }
      mid = Math.floor((low + high) / 2);
    }
    return mid;
  };
};

